package com.example.coinhelper.Activity;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.example.coinhelper.Bean.User;
import com.example.coinhelper.Manage.UserManager;
import com.example.coinhelper.R;
import com.example.coinhelper.utils.MySesseion;
import com.example.coinhelper.utils.TimeUtility;
import com.google.android.material.snackbar.Snackbar;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private TextView textViewRegister;
    private TextView textUsername;
    private TextView textPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        textUsername = findViewById(R.id.editTextUsername);
        textPassword = findViewById(R.id.editTextPassword);
        textViewRegister = findViewById(R.id.textViewRegister);
        textViewRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 这里添加启动注册页面的代码
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
        Button buttonLogin = findViewById(R.id.buttonLogin);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = textUsername.getText().toString();
                String password = textPassword.getText().toString();
                if("".equals(username)||"".equals(password)||username==null||password==null){
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                User user = new User(username,password);
                UserManager userManager = new UserManager(LoginActivity.this); // 假设 UserManager 在当前活动中可用
                User loggedInUser = userManager.login(user);
                if (loggedInUser != null) {
                    String str = TimeUtility.getCurrentLocalTime();
                    Log.e(TAG, "登录成功");
                    MySesseion.USER.setUsername(username);
                    MySesseion.USER.setPassword(password);
                    MySesseion.USER.setRegistrationTime(loggedInUser.getRegistrationTime());

                    loggedInUser.setRegistrationTime(str);
                    userManager.updateUser(loggedInUser);
                    System.out.println(MySesseion.USER.toString());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();
                    Log.e(TAG, "账号或密码错误");
                }
            }
        });
    }
}
